﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RonaldCamcam.XBeeFramework.Api;
using System.IO.Ports;
using RonaldCamcam.XBeeApiFramework.Utilility;

namespace RonaldCamcam.XBeeFramework
{
    public class XBee
    {
        public const int WAIT_AFTER_WRITE_TIME = 50; //50 milliseconds

        public string ComPort { get; set; }
        public int PortBaudRate { get; set; }
        public Parity PortParity { get; set; }
        public int PortDataBits { get; set; }
        public StopBits PortStopBits { get; set; }
        public int ReadTimeOut { get; set; }
        public int WriteTimeout { get; set; }

        public Response CommandResponse { get; set; }

        public XBee(string comPort, int baudRate, Parity parity, int dataBits, StopBits stopBits)
        {
            ComPort = comPort;
            PortBaudRate = baudRate;
            PortParity = parity;
            PortDataBits = dataBits;
            PortStopBits = stopBits;

            CommandResponse = new Response();
        }
        public XBee(string comPort, int baudRate, Parity parity, int dataBits, StopBits stopBits, int readTimeOut, int writeTimeOut)
        {
            ComPort = comPort;
            PortBaudRate = baudRate;
            PortParity = parity;
            PortDataBits = dataBits;
            PortStopBits = stopBits;
            ReadTimeOut = readTimeOut;
            WriteTimeout = writeTimeOut;

            CommandResponse = new Response();
        }

        void FinishedReceivingData(ApiCommand ac)
        {                        
            //check if there are errors
            if (string.IsNullOrEmpty(ac.PortStatus))
            {
                //Parse ac.ReceivedData
                CommandResponse = ResponseFactory.Parse(ac.ReceivedData, ac.Encoding);                               
            }
            else
            {
                Console.WriteLine("Message while Receiving data: {0}", ac.PortStatus);
            }            
        }        

        public Response Send(ApiCommand command, byte[] parameter)
        {
            command.DataReceivedCompleted += new ApiCommand.DataReceived(FinishedReceivingData);
            command.SendToXBee(parameter);
            return CommandResponse;
        }

        public Response SendRemote(ApiRemoteCommand command, byte[] parameter, ApplyRemoteChanges applyRemoteChanges)
        {
            command.SendToXBee(parameter, applyRemoteChanges);
            command.DataReceivedCompleted += new ApiRemoteCommand.DataReceived(FinishedReceivingRemoteData);
            return CommandResponse;            
        }

        void FinishedReceivingRemoteData(ApiRemoteCommand ac)
        {
            //check if there are errors
            if (string.IsNullOrEmpty(ac.PortStatus))
            {
                //Parse ac.ReceivedData
                CommandResponse = ResponseFactory.ParseRemoteData(ac.ReceivedData, ac.Encoding);
            }
            else
            {
                Console.WriteLine("Message while Receiving data: {0}", ac.PortStatus);
            }  
        }        
    }
}
